<?php

namespace App\Admin\Controllers;

use App\Models\Posts;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\MessageBag;

class PostsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.posts'))
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.posts'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.posts'))
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.posts'))
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Posts);

        $grid->id('Id');
        $grid->object_name(trans('admin.object_name'))->display(function () {
            return $this->object->username;
        });
        $grid->title(trans('admin.title'))->modal(trans('admin.comments'), function ($model) {

            $comments = $model->comments()->with('allChildren')->get()->map(function ($comment) {

                return $comment->only(['id', 'content', 'created_at']);

            });

            return new Table(['ID', trans('admin.content'), trans('admin.created_at')], $comments->toArray());
        });
        $grid->subtitle(trans('admin.subtitle'));
        $grid->pictures(trans('admin.pictures'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->content(trans('admin.content'))->limit(10);
        $grid->view_amount(trans('admin.view_amount'))->badge('light');
        $grid->like_amount(trans('admin.like_amount'))->badge('red');
        $grid->collect_amount(trans('admin.collect_amount'))->badge('yellow');
        $grid->is_show(trans('admin.show'))->switch();
        $grid->created_at(trans('admin.created_at'));
        $grid->updated_at(trans('admin.updated_at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Posts::findOrFail($id));

        $show->id('Id');
        $show->object_name(trans('admin.object_name'));
        $show->object_type(trans('admin.object_type'));
        $show->title(trans('admin.title'));
        $show->subtitle(trans('admin.subtitle'));
        $show->pictures(trans('admin.pictures'))->image();
        $show->content(trans('admin.content'))->unescape()->as(function ($text) {
            return $text;
        });
        $show->view_amount(trans('admin.view_amount'));
        $show->like_amount(trans('admin.like_amount'));
        $show->collect_amount(trans('admin.collect_amount'));
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Posts);

        $form->text('title', trans('admin.title'))->rules('required')->setLabelClass(['required']);
        $form->text('subtitle', trans('admin.subtitle'));
        $form->multipleImage('pictures', trans('admin.pictures'));
        $form->editor('content', trans('admin.content'));
        $form->number('view_amount', trans('admin.view_amount'))->default(0)->min(0);
        $form->number('like_amount', trans('admin.like_amount'))->default(0)->min(0);
        $form->number('collect_amount', trans('admin.collect_amount'))->default(0)->min(0);
//        $form->number('sort', trans('admin.sort'))->default(0);
        $form->switch('is_show', trans('admin.show'))->default(1);
        $form->saving(function ($form) {
            $form->model()->object_id = Admin::user()->id;
            $form->model()->object_type = 'App\Models\AdminUser';

            //判断图片的数量
            if (is_array($form->pictures) && count($form->pictures) > 9) {
                $error = new MessageBag([
                    'title' => trans('admin.failed'),
                    'message' => 'to much pictures(9 at most )',
                ]);

                return back()->with(compact('error'));
            }
        });

        return $form;
    }

}
